package com.zhentao.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zhentao.common.Result;
import com.zhentao.domain.Area;
import com.zhentao.domain.Province;
import com.zhentao.domain.SysSite;
import com.zhentao.domain.vo.SiteSeataData;
import com.zhentao.service.ProvinceService;
import com.zhentao.service.SysSiteService;
import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: ProvinceController
 * @Desrciption: Mr.chengfenzhan
 * @Author: 知安_程烽展
 * @Date: 2024/9/19 10:46
 */
@RestController
@RequestMapping("/province")
public class ProvinceController {
    @Autowired
    private ProvinceService provinceService;

    @Autowired
    private SysSiteService sysSiteService;

    @Autowired
    private StringRedisTemplate redis;

    @RequestMapping("/list")
    public Result getList(){
        String areaData = redis.opsForValue().get("ProvinceData");
        List<SiteSeataData> list=new ArrayList<SiteSeataData>();
        List<Province> siteSeataData1 = JSON.parseArray(areaData, Province.class);
        for (Province province  : siteSeataData1) {
            SiteSeataData siteSeataData = new SiteSeataData();
            siteSeataData.setName(province.getName());
            List<SysSite> list1 = sysSiteService.list(Wrappers.<SysSite>lambdaQuery().eq(SysSite::getProvinceId, province.getId()));
            siteSeataData.setValue((long) list1.size());
            list.add(siteSeataData);
        }
        return Result.build(list, 200, "操作成功");
    }

    @PostConstruct
    public void init(){
        redis.opsForValue().set("ProvinceData", JSON.toJSONString(provinceService.list()));
    }
}
